package com.sh.rbac.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.rbac.core.common.exception.CustomException;
import com.sh.rbac.core.common.util.ExcelUtils;
import com.sh.rbac.core.mybatis.annotation.DataPermission;
import com.sh.rbac.core.common.result.ResultCodeEnum;
import com.sh.rbac.domain.converter.OperationLogConverter;
import com.sh.rbac.domain.dto.operationLog.OperationLogQueryDTO;
import com.sh.rbac.domain.entity.OperationLog;
import com.sh.rbac.domain.vo.operationLog.OperationLogEeVO;
import com.sh.rbac.domain.vo.operationLog.OperationLogVO;
import com.sh.rbac.handler.OperationLogCellStyleHandler;
import com.sh.rbac.mapper.OperationLogMapper;
import com.sh.rbac.service.OperationLogService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lucky
 * @description 针对表【sys_operation_log(操作日志记录表)】的数据库操作Service实现
 * @createDate 2024-07-07 17:50:48
 */
@Slf4j
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog>
        implements OperationLogService {

    @DataPermission(userColumn = "user_id")
    @Override
    public void checkDataScope(List<Long> ids) {
        LambdaQueryWrapper<OperationLog> lqw = new LambdaQueryWrapper<>();
        lqw.in(OperationLog::getId, ids);

        long count = count(lqw);
        if(count != ids.size()){
            throw new CustomException(ResultCodeEnum.NOT_DATA_SCOPE);
        }
    }

    @DataPermission(userColumn = "user_id")
    @Override
    public Page<OperationLogVO> getPage(Long current, Long limit, OperationLogQueryDTO operationLogQueryDTO) {
        // 创建 page 对象，传递当前页, 每页记录数
        Page<OperationLog> page = new Page<>(current, limit);

        // 条件参数
        String username = operationLogQueryDTO.getUsername();
        String operationModule = operationLogQueryDTO.getOperationModule();
        Integer operationType = operationLogQueryDTO.getOperationType();
        Integer operationStatus = operationLogQueryDTO.getOperationStatus();

        String startTime = null;
        String endTime = null;
        if (operationLogQueryDTO.getOperationTimes() != null) {
            startTime = operationLogQueryDTO.getOperationTimes()[0];
            endTime = operationLogQueryDTO.getOperationTimes()[1];
        }

        // 构建条件
        LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(username), OperationLog::getUsername, username)
                .like(StrUtil.isNotBlank(operationModule), OperationLog::getOperationModule, operationModule)
                .eq(operationType != null, OperationLog::getOperationType, operationType)
                .eq(operationStatus != null, OperationLog::getOperationStatus, operationStatus)
                .between(StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime), OperationLog::getOperationTime, startTime, endTime);

        // 根据修改时间倒序
        queryWrapper.orderByDesc(OperationLog::getOperationTime);

        // 查询数据
        Page<OperationLog> operationLogPage = baseMapper.selectPage(page, queryWrapper);

        // 转换数据
        Page<OperationLogVO> operationLogVO = new Page<>();
        operationLogVO.setTotal(operationLogPage.getTotal());
        operationLogVO.setSize(operationLogPage.getSize());
        operationLogVO.setCurrent(operationLogPage.getCurrent());
        operationLogVO.setRecords(OperationLogConverter.INSTANCE.convertToOperationLogVOList(operationLogPage.getRecords()));
        operationLogVO.setPages(operationLogPage.getPages());

        return operationLogVO;
    }

    @DataPermission(userColumn = "user_id")
    @Override
    public void exportData(Integer startCount, Integer endCount, HttpServletResponse response) {
        Page<OperationLog> page = new Page<>(startCount, endCount);
        List<OperationLog> operationLogs = baseMapper.selectPage(page, null).getRecords();
        List<OperationLogVO> operationLogVOList = OperationLogConverter.INSTANCE.convertToOperationLogVOList(operationLogs);
        ExcelUtils.exportDataToExcel(response, "操作日志", OperationLogEeVO.class, new OperationLogCellStyleHandler(), operationLogVOList);
    }
}




